{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基于 Python 的模块和线性图\n",
    "\n",
    "#### 一、Python 模块\n",
    "##### 1.模块的定义\n",
    "python 模块（module）是一个 Python 文件，以 .py 结尾，包含了 Python 对象定义和 Python 语句。\n",
    "\n",
    "模块让你能够有逻辑地组织你的 Python 代码段。把相关的代码分配到一个模块里可以让代码更好看，同时能够让其具备一定的可复用性。\n",
    "\n",
    "模块能定义函数，类和变量，模块里也能包含可执行的代码。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以下是一个简单的模块 support.py,平台中将文件进行了重命名\n",
    "!cat /data/shixunfiles/f8b8ed0a80c506f2a932428a119dd51b_1606276467113.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 2.模块的引入\n",
    "python 的模块引入分为三种方法：\n",
    "- import modulename\n",
    "\n",
    "导入上述的模块为：\n",
    "import support\n",
    "\n",
    "- from ... import modulename\n",
    "\n",
    "导入上述的模块为：\n",
    "from support import print_func\n",
    "\n",
    "- 添加到搜索路径中\n",
    "\n",
    "当你导入一个模块，Python 解析器对模块位置的搜索顺序为：\n",
    "\n",
    "1) 当前目录；\n",
    "\n",
    "2) 如果不在当前目录，Python 则搜索在 shell 变量 PYTHONPATH 下的每个目录；\n",
    "\n",
    "3) 如果都找不到，Python 会查看默认路径。UNIX 下，默认路径一般为 /usr/local/lib/python。\n",
    "\n",
    "当导入的模块在不同的目录下时，需在引入前加入下述语句：\n",
    "\n",
    "```\n",
    "import sys\n",
    "sys.path.append(\"path_to_module\")\n",
    "```\n",
    "\n",
    "> Notice：该方法只会在 Python 文件中临时生效，并不会永久将该路径添加进搜索路径中，path_to_module 为将要引入的库所在路径。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将模块所在路径添加进搜索路径中\n",
    "import sys\n",
    "sys.path.append(\"/data/shixunfiles/\")\n",
    "\n",
    "\n",
    "# 采用 import modulename 的方式导入模块\n",
    "import f8b8ed0a80c506f2a932428a119dd51b_1606276467113 as support\n",
    "support.print_func(\"my world\")\n",
    "\n",
    "# 采用from ... import modulename\n",
    "# from support import print_func\n",
    "# print_func(\"my world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 二、线性图绘制\n",
    "线性图的绘制通常分为以下几个步骤：\n",
    "- 1.载入数据\n",
    "- 2.数据的预处理\n",
    "- 3.图形化及标注"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 载入相应的库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 载入数据\n",
    "# 使用数据集中的data.csv\n",
    "data = pd.read_csv(\"/data/shixunfiles/81d2e8ac83cd6a957065e70a2b18d65b_1606276467115.csv\")\n",
    "print(data.head(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据中各列说明如下：\n",
    "\n",
    "|   列名| 类型  |注释   |\n",
    "| ------------ | ------------ | ------------ |\n",
    "|  Date | datetime64   | 日期  |\n",
    "|  Value| float64  | 数值   |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据处理：将日期格式进行转化\n",
    "data['Date'] = pd.to_datetime(data['Date'])\n",
    "\n",
    "# 数据格式显示\n",
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# jupyter 中使用 matplotlib 显示图片，默认使用一个内存地址，无法正常显示\n",
    "# 使用该指令能够正常显示图片\n",
    "%matplotlib inline\n",
    "\n",
    "# 导入 matplotlib 库\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 没有传入数据的时候是画一张空白的图\n",
    "plt.plot()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 取数据的前几条进行实验\n",
    "first_ten = data[0:10]\n",
    "\n",
    "# 绘图\n",
    "# plt.plot(x,y)其中第一个参数为横坐标，第二个为纵坐标\n",
    "plt.plot(first_ten['Date'],first_ten['Value'])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上述绘制的图中会发现横坐标的 label 发生了重合\n",
    "plt.plot(first_ten['Date'],first_ten['Value'])\n",
    "# 将 label 旋转 45 度\n",
    "plt.xticks(rotation=45)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加轴标签、表头等属性\n",
    "plt.plot(data['Date'],data['Value'])\n",
    "# 旋转横坐标 label\n",
    "plt.xticks(rotation=45)\n",
    "# 旋转纵坐标 label\n",
    "plt.yticks(rotation=30)\n",
    "# 给横坐标定义名称\n",
    "plt.xlabel('Month')\n",
    "# 给纵坐标定义名称\n",
    "plt.ylabel('Value')\n",
    "# 给图形添加标题\n",
    "plt.title('Test_drawing')\n",
    "# 显示图形\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制子图\n",
    "fig = plt.figure()\n",
    "# fig.add_subplot(x,y,z)，绘制的图在x行，y列，即从左往右，从上往下数的第 z 个位置\n",
    "\n",
    "ax1 = fig.add_subplot(3,3,1)\n",
    "ax2 = fig.add_subplot(3,3,5)\n",
    "ax3 = fig.add_subplot(3,3,9)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上述绘制的图出现部分子图重合的情况\n",
    "# 定义图形的大小\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "ax1 = fig.add_subplot(3,3,1)\n",
    "ax2 = fig.add_subplot(3,3,5)\n",
    "ax3 = fig.add_subplot(3,3,9)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义子图\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "ax1 = fig.add_subplot(3,3,1)\n",
    "ax2 = fig.add_subplot(3,3,5)\n",
    "ax3 = fig.add_subplot(3,3,9)\n",
    "\n",
    "# 对子图进行绘制\n",
    "first_ten = data[0:10]\n",
    "second_ten = data[10:20]\n",
    "last = data[20:]\n",
    "\n",
    "# 对第一个子图进行绘制\n",
    "ax1.plot(first_ten['Date'],first_ten['Value'])\n",
    "# 将第一个子图的横坐标的标签旋转\n",
    "ax1.tick_params(labelrotation=45)\n",
    "\n",
    "# 对第二个子图进行绘制\n",
    "ax2.plot(second_ten['Date'],second_ten['Value'])\n",
    "# 将第二个子图的横坐标的标签旋转\n",
    "ax2.tick_params(labelrotation=45)\n",
    "\n",
    "# 对第三个子图进行绘制\n",
    "ax3.plot(last['Date'],last['Value'])\n",
    "# 将第三个子图的横坐标的标签旋转\n",
    "ax3.tick_params(labelrotation=45)\n",
    "\n",
    "# 注意此处没有使用以下\n",
    "# plt.xticks(rotation=45)\n",
    "# 该方法不会对所有的子图生效，仅作用于最后一个子图\n",
    "\n",
    "# 显示图形\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将要载入的数据各列说明如下：\n",
    "\n",
    "|   列名| 类型  |注释   |\n",
    "| ------------ | ------------ | ------------ |\n",
    "|  Month | int64   | 月份  |\n",
    "|  Value| float64  | 数值   |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 多线绘制\n",
    "# 载入数据\n",
    "# 使用数据集中的Month.csv\n",
    "new_data = pd.read_csv('/data/shixunfiles/bbff16179d37f025294612af86cba18d_1606276467110.csv')\n",
    "# 显示数据的前五行\n",
    "print(new_data.head(5))\n",
    "# 显示数据格式\n",
    "new_data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义图形大小\n",
    "fig = plt.figure(figsize=(8,5))\n",
    "# 绘制第一年的数据\n",
    "# 使用的参数为横坐标，纵坐标，颜色\n",
    "# 其中颜色r代表red红色\n",
    "plt.plot(new_data['Month'][0:12],new_data['Value'][0:12],c='r')\n",
    "# 绘制第二年的数据\n",
    "# 使用的参数为横坐标，纵坐标，颜色\n",
    "# 其中颜色g代表green绿色\n",
    "plt.plot(new_data['Month'][12:24],new_data['Value'][12:24],c='g')\n",
    "# 对不同曲线进行 label 标记\n",
    "plt.legend(['the first year','the second year'])\n",
    "# 标记横纵坐标\n",
    "plt.xlabel('Month')\n",
    "plt.ylabel('Value')\n",
    "# 标记标题\n",
    "plt.title('Value to Month')\n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "至此，基于 Python 的模块和线性图就结束了。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
